class Solution:
    def semiOrderedPermutation(self, nums: List[int]) -> int:
        if nums == []:
            return 0
        n = len(nums)
        a = nums.index(1)
        b = nums.index(n)
        return a-0+n-b-1 - (a>b)

if __name__ == '__main__':
    s = Solution()
    print(s.semiOrderedPermutation([1,2,3,4,5]))
    print(s.semiOrderedPermutation([2,1,4,3]))
    print(s.semiOrderedPermutation([2,4,1,3]))